﻿Imports System.Data.OleDb

Public Class testPage
    Inherits System.Web.UI.Page
    Public Shared imagesquares(81) As String
    Public Shared pics(81) As String
    Public Shared CakeNum(81) As Integer

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load


    End Sub

    Function makeWall()
        Dim picker As System.Random = New System.Random
        Dim whattouse As Integer = 1
        Dim gh As Integer = picker.Next(1, 81)
        Dim Pic As String
        Dim cid As Integer

        'Clear any preconsiting
        For i = 1 To 81
            imagesquares(i) = Nothing
        Next

        'First Picture (5 by 5)
        cid = CakeNum(whattouse)
        Pic = pics(whattouse)
        whattouse = whattouse + 1
        Do While checkaccess(5, gh) = False
            gh = picker.Next(1, 81)
        Loop
        fillgrid(gh, 5, Pic, cid)

        'Second Picture (4 by 4)
        
        Dim goodtogo As Boolean = False
        Dim nogo As Boolean = False
        Dim timeout As Integer = 10000000
        While goodtogo = False
            gh = picker.Next(1, 81)
            If checkaccess(4, gh) Then
                If checkloco(4, gh) Then
                    goodtogo = True
                End If
            End If
            timeout = timeout - 1
            If timeout <= 0 Then
                goodtogo = True
                nogo = True
            End If
        End While
        If nogo = False Then
            cid = CakeNum(whattouse)
            Pic = pics(whattouse)
            whattouse = whattouse + 1
            fillgrid(gh, 4, Pic, cid)
        End If

        'Third Picture (3 by 3)
        goodtogo = False
        nogo = False
        timeout = 10000000
        While goodtogo = False
            gh = picker.Next(1, 81)
            If checkaccess(3, gh) Then
                If checkloco(3, gh) Then
                    goodtogo = True
                End If
            End If
            timeout = timeout - 1
            If timeout <= 0 Then
                goodtogo = True
                nogo = True
            End If
        End While
        If nogo = False Then
            cid = CakeNum(whattouse)
            Pic = pics(whattouse)
            whattouse = whattouse + 1
            fillgrid(gh, 3, Pic, cid)
        End If

        '4th Picture 3 by 3
        goodtogo = False
        nogo = False
        timeout = 10000000
        While goodtogo = False
            gh = picker.Next(1, 81)
            If checkaccess(3, gh) Then
                If checkloco(3, gh) Then
                    goodtogo = True
                End If
            End If
            timeout = timeout - 1
            If timeout <= 0 Then
                goodtogo = True
                nogo = True
            End If
        End While
        If nogo = False Then
            cid = CakeNum(whattouse)
            Pic = pics(whattouse)
            whattouse = whattouse + 1
            fillgrid(gh, 3, Pic, cid)
        End If

        '5th Pciture 2 by 2
        goodtogo = False
        nogo = False
        timeout = 10000000
        While goodtogo = False
            gh = picker.Next(1, 81)
            If checkaccess(2, gh) Then
                If checkloco(2, gh) Then
                    goodtogo = True
                End If
            End If
            timeout = timeout - 1
            If timeout <= 0 Then
                goodtogo = True
                nogo = True
            End If
        End While
        If nogo = False Then
            cid = CakeNum(whattouse)
            Pic = pics(whattouse)
            whattouse = whattouse + 1
            fillgrid(gh, 2, Pic, cid)
        End If

        '6th Picture 2 by 2
        goodtogo = False
        nogo = False
        timeout = 10000000
        While goodtogo = False
            gh = picker.Next(1, 81)
            If checkaccess(2, gh) Then
                If checkloco(2, gh) Then
                    goodtogo = True
                End If
            End If
            timeout = timeout - 1
            If timeout <= 0 Then
                goodtogo = True
                nogo = True
            End If
        End While
        If nogo = False Then
            cid = CakeNum(whattouse)
            Pic = pics(whattouse)
            whattouse = whattouse + 1
            fillgrid(gh, 2, Pic, cid)
        End If

        '7th Pciture 2 by 2
        goodtogo = False
        nogo = False
        timeout = 10000000
        While goodtogo = False
            gh = picker.Next(1, 81)
            If checkaccess(2, gh) Then
                If checkloco(2, gh) Then
                    goodtogo = True
                End If
            End If
            timeout = timeout - 1
            If timeout <= 0 Then
                goodtogo = True
                nogo = True
            End If
        End While
        If nogo = False Then
            cid = CakeNum(whattouse)
            Pic = pics(whattouse)
            whattouse = whattouse + 1
            fillgrid(gh, 2, Pic, cid)
        End If

        'Fill in the gaps
        Dim l As Integer
        For l = 1 To 81
            If imagesquares(l) = Nothing Then
                cid = CakeNum(whattouse)
                Pic = pics(whattouse)
                whattouse = whattouse + 1
                imagesquares(l) = "<div class=crop><a href=details.aspx?cakeNo=" & cid & "><img src=" & Chr(34) & Pic & Chr(34) & " style=width:100px; height:100px;></a></div>"
            End If
        Next
        Dim outputHTML As String = ""
        For l = 1 To 81
            outputHTML = outputHTML & imagesquares(l)
        Next
        Label1.Text = outputHTML
        Return True

    End Function

    Function checkaccess(ByVal ilength As Integer, ByVal SS As Integer)
        'Check Across
        Dim i As Integer = SS + (ilength - 1)
        Select Case SS
            Case 1, 2, 3, 4, 5, 6, 7, 8, 9
                If i > 9 Then Return False
            Case 9, 10, 11, 12, 13, 14, 15, 16, 17, 18
                If i > 18 Then Return False
            Case 19, 20, 21, 22, 23, 24, 25, 26, 27
                If i > 27 Then Return False
            Case 28, 29, 30, 31, 32, 33, 34, 35, 36
                If i > 36 Then Return False
            Case 37, 38, 39, 40, 41, 42, 43, 44, 45
                If i > 45 Then Return False
            Case 46, 47, 48, 49, 50, 51, 52, 53, 54
                If i > 54 Then Return False
            Case 55, 56, 57, 58, 59, 60, 61, 62, 63
                If i > 63 Then Return False
            Case 64, 65, 66, 67, 68, 69, 70, 71, 72
                If i > 72 Then Return False
            Case 73, 74, 75, 76, 77, 78, 79, 80, 81
                If i > 81 Then Return False
        End Select

        'Check Down
        Dim x As Integer = SS + ((ilength - 1) * 9)
        If x > 81 Then Return False

        Return True

    End Function

    Function fillgrid(ByVal ss As Integer, ByVal len As Integer, ByVal pic As String, ByVal cid As Integer)
        Dim WandH As Integer = len * 100

        'Create The Rows
        Dim rowloop As Integer
        Dim colloop As Integer
        Dim marginT As Integer
        Dim marginL As Integer
        Dim sss As Integer = ss
        'Row 1

        For rowloop = 0 To len - 1
            marginT = rowloop * 100
            For colloop = 0 To len - 1
                marginL = colloop * 100
                sss = ss + colloop
                imagesquares(sss) = "<div class=crop><a href=details.aspx?cakeNo=" & cid & "><img src=" & Chr(34) & pic & Chr(34) & "style=" & Chr(34) & "width:" & WandH & "px; height:" & WandH & "px; margin-left:-" & marginL & "px; margin-top:-" & marginT & "px;" & Chr(34) & "></a></div>"
            Next
            ss = ss + 9
        Next
        Return True

    End Function

    Function checkloco(ByVal len As Integer, ByVal ss As Integer)
        Dim rowloop As Integer
        Dim colloop As Integer
        Dim sss As Integer = ss

        For rowloop = 0 To len - 1
            For colloop = 0 To len - 1
                sss = ss + colloop
                If imagesquares(sss) = Nothing Then
                    'A ok
                Else
                    Return False
                End If
            Next
            ss = ss + 9
        Next

        Return True
    End Function


    Protected Sub btn_pop_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_pop.Click
        Dim SQL As String = "SELECT TOP 50 [ID], [CountOfComment], [CountOfpageHitIP], [CakePictureMain], [CountOfCT_FID], [TotalPop] FROM [CakePop2] ORDER BY [TotalPop] DESC"
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("CakeConnection").ConnectionString)
        Dim cmd As OleDbCommand = New OleDbCommand(SQL, conn)
        conn.Open()
        Dim dr As OleDbDataReader = cmd.ExecuteReader()
        dr.Read()
        Dim x As Integer = 1
        If dr.HasRows Then
            Do While dr.Read()
                pics(x) = dr.GetString(3)
                CakeNum(x) = dr.GetInt32(0)
                x = x + 1
            Loop
            Dim i As Integer
            For i = x To 81
                pics(x) = "Pictures/cakewall.png"
                CakeNum(x) = 100
            Next

        End If
        makeWall()
    End Sub

    Protected Sub btn_random_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_random.Click
        Dim Sql As String = "SELECT * FROM Cakes ORDER BY  RND([ID]-Timer())"
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("CakeConnection").ConnectionString)
        Dim cmd As OleDbCommand = New OleDbCommand(Sql, conn)
        conn.Open()
        Dim dr As OleDbDataReader = cmd.ExecuteReader()
        dr.Read()
        Dim x As Integer = 1
        If dr.HasRows Then
            Do While dr.Read()
                pics(x) = dr.GetString(5)
                CakeNum(x) = dr.GetInt32(0)
                x = x + 1
            Loop
            Dim i As Integer
            For i = x To 81
                pics(x) = "Pictures/cakewall.png"
                CakeNum(x) = 100
            Next

        End If
        makeWall()

    End Sub
End Class